/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AppSelector.java
 *
 * Created on Nov 8, 2012, 9:25:41 AM
 */
package UserInterface;

import insurface.InteractiveSurface;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import setting.InitialSettings;
import setting.SurfaceSettings;

/**
 *
 * @author Home
 */
public class InitialScreen extends javax.swing.JFrame implements PropertyChangeListener {

    /** Creates new form AppSelector */
    insurface.InteractiveSurface is = new InteractiveSurface();
    setting.InitialSettings iss = null;
    setting.SurfaceSettings ss = null;
    InitialSettings inits = null;
    boolean ctd = false;
    boolean fs = false;
    boolean cs = false;

    public InitialScreen() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        jPanel6 = new javax.swing.JPanel();
        buttonGroup4 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        fhwp = new javax.swing.JRadioButton();
        pp = new javax.swing.JRadioButton();
        fh = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        rdInit = new javax.swing.JRadioButton();
        rdLastCam = new javax.swing.JRadioButton();
        rdFileCam = new javax.swing.JRadioButton();
        jPanel3 = new javax.swing.JPanel();
        btNext = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        rdCal = new javax.swing.JRadioButton();
        rdLastCal = new javax.swing.JRadioButton();
        rdFileCal = new javax.swing.JRadioButton();
        jPanel7 = new javax.swing.JPanel();
        FingerButton = new javax.swing.JRadioButton();
        redButton = new javax.swing.JRadioButton();

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Select Application");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Application", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon.sleek_pencil.jpg"))); // NOI18N

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/powerpoint.png"))); // NOI18N

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/presentation_icon_01.jpg"))); // NOI18N

        buttonGroup1.add(fhwp);
        fhwp.setFont(new java.awt.Font("Tahoma", 0, 12));
        fhwp.setSelected(true);
        fhwp.setText("Free Hand Writing (With Projector)");
        fhwp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fhwpActionPerformed(evt);
            }
        });

        buttonGroup1.add(pp);
        pp.setFont(new java.awt.Font("Tahoma", 0, 12));
        pp.setText("Power Point");
        pp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ppActionPerformed(evt);
            }
        });

        buttonGroup1.add(fh);
        fh.setFont(new java.awt.Font("Tahoma", 0, 12));
        fh.setText("Free Hand Drawing");
        fh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fhActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(fhwp))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(jLabel6)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(fh)
                    .addComponent(jLabel4))
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(pp))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(fhwp)
                            .addComponent(fh)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pp)
                        .addGap(5, 5, 5)))
                .addContainerGap())
        );

        jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPanel2.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);

        jLabel2.setFont(new java.awt.Font("Perpetua Titling MT", 3, 24));
        jLabel2.setForeground(new java.awt.Color(0, 0, 204));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/GroupIcon.jpg"))); // NOI18N
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 541, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(17, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 126, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Cameras", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        buttonGroup2.add(rdInit);
        rdInit.setFont(new java.awt.Font("Tahoma", 1, 12));
        rdInit.setSelected(true);
        rdInit.setText("Select Cameras");
        rdInit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdInitActionPerformed(evt);
            }
        });

        buttonGroup2.add(rdLastCam);
        rdLastCam.setFont(new java.awt.Font("Tahoma", 1, 12));
        rdLastCam.setText("Last settings");
        rdLastCam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdLastCamActionPerformed(evt);
            }
        });

        buttonGroup2.add(rdFileCam);
        rdFileCam.setFont(new java.awt.Font("Tahoma", 1, 12));
        rdFileCam.setText("Load from file");
        rdFileCam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdFileCamActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rdLastCam)
                            .addComponent(rdInit))
                        .addContainerGap())
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(rdFileCam, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(16, 16, 16))))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(rdInit)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdLastCam, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rdFileCam, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Operation", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        btNext.setFont(new java.awt.Font("Tahoma", 1, 12));
        btNext.setText("Next");
        btNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNextActionPerformed(evt);
            }
        });

        exitButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        exitButton.setText("Exit");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(btNext, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addComponent(exitButton))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btNext)
                    .addComponent(exitButton))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Calibration", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        buttonGroup3.add(rdCal);
        rdCal.setFont(new java.awt.Font("Tahoma", 1, 12));
        rdCal.setSelected(true);
        rdCal.setText("Calibrate");
        rdCal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdCalActionPerformed(evt);
            }
        });

        buttonGroup3.add(rdLastCal);
        rdLastCal.setFont(new java.awt.Font("Tahoma", 1, 12));
        rdLastCal.setText("Last settings");
        rdLastCal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdLastCalActionPerformed(evt);
            }
        });

        buttonGroup3.add(rdFileCal);
        rdFileCal.setFont(new java.awt.Font("Tahoma", 1, 12));
        rdFileCal.setText("Load from file");
        rdFileCal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdFileCalActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rdCal)
                    .addComponent(rdLastCal)
                    .addComponent(rdFileCal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(rdCal)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdLastCal, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rdFileCal, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Stylus", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        buttonGroup4.add(FingerButton);
        FingerButton.setFont(new java.awt.Font("Arial", 1, 12));
        FingerButton.setSelected(true);
        FingerButton.setText("Finger");
        FingerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FingerButtonActionPerformed(evt);
            }
        });

        buttonGroup4.add(redButton);
        redButton.setFont(new java.awt.Font("Arial", 1, 12));
        redButton.setText("Red Stylus");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(FingerButton)
                    .addComponent(redButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(FingerButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(redButton)
                .addContainerGap(29, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void rdInitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdInitActionPerformed
        if (rdInit.isSelected() && fhwp.isSelected()) {
            rdLastCal.setEnabled(false);
            rdFileCal.setEnabled(false);
            rdCal.setSelected(true);
        }

        // TODO add your handling code here:
}//GEN-LAST:event_rdInitActionPerformed
    int i = 0;
    private void btNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNextActionPerformed
        if (fhwp.isSelected()) {
            i = 1;
        }
        if (fh.isSelected()) {
            i = 2;
        }
        if (pp.isSelected()) {
            i = 3;
        }
        try {
            // TODO add your handling code here:
            FileOutputStream fos = new FileOutputStream("settings.ser");
            try {
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                boolean fingerSelected = FingerButton.isSelected();
                boolean camSelected = rdInit.isSelected();
                boolean needtocalibrate = rdCal.isSelected();
                boolean loadCam = rdLastCam.isSelected();
                boolean fileCam = rdFileCam.isSelected();
                boolean loadCal = rdLastCal.isSelected();
                boolean fileCal = rdFileCal.isSelected();
                int application = i;
                iss = new InitialSettings(fingerSelected, camSelected, needtocalibrate, application, loadCam, fileCam, loadCal, fileCal);
                oos.writeObject(iss);
                oos.close();
                fos.close();
            } catch (IOException ex) {
                gotoSetCam();
                System.out.println("error, moving to set cameras");
            }

        } catch (FileNotFoundException ex) {
            gotoSetCam();
            System.out.println("error, moving to set cameras");
        }

        if (rdInit.isSelected()) {
            this.setVisible(false);
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Dimension dim = toolkit.getScreenSize();

            SetCameras camSet = new SetCameras(i, 0);
            int x_location = (dim.width - camSet.getBounds().width) / 2;
            int y_location = (dim.height - camSet.getBounds().height) / 2;
            camSet.setLocation(x_location, y_location);
            camSet.setVisible(true);
            camSet.setIs(iss);

        } else if (rdLastCam.isSelected()) {
            ss = null;
            try {
                FileInputStream fis = new FileInputStream("FinalSettings.ser");
                try {
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    try {
                        ss = (SurfaceSettings) ois.readObject();
                        ois.close();
                        fis.close();
                    } catch (ClassNotFoundException ex) {
                        gotoSetCam();
                        System.out.println("error, moving to set cameras");
                    }
                } catch (IOException ex) {
                    gotoSetCam();
                    System.out.println("error, moving to set cameras");
                }
            } catch (FileNotFoundException ex) {
                gotoSetCam();
                System.out.println("error, moving to set cameras");
            }
            if (rdCal.isSelected()) {
                gotoapp(iss);
            } else if (rdLastCal.isSelected()) {
                is.setSettings(ss);
                is.setRight_Camera(ss.getRightCam());
                is.setLeft_Camera(ss.getLeftCam());
                is.setIsettings(iss);
                gotoapp(iss);
            } else if (!(rdCal.isSelected() || rdLastCal.isSelected() || rdFileCam.isSelected())) {
                gotoapp(iss);
            }
        } else if (rdFileCam.isSelected()) {
            JFileChooser jfc = new JFileChooser();
            int returnVal = jfc.showOpenDialog(rootPane);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = jfc.getSelectedFile();
                FileInputStream fis = null;
                //SurfaceSettings sst = null;
                SetCameras sc = null;
                try {
                    fis = new FileInputStream(file);
                } catch (FileNotFoundException ex) {
                    gotoSetCam();
                    System.out.println("error, moving to set cameras");
                }
                try {
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    try {
                        ss = (SurfaceSettings) ois.readObject();
                        ois.close();
                        fis.close();
                    } catch (ClassNotFoundException ex) {
                        gotoSetCam();
                        System.out.println("error, moving to set cameras");
                    }
                } catch (IOException ex) {
                    gotoSetCam();
                    System.out.println("error, moving to set cameras");
                }
                if (rdCal.isSelected()) {
                    gotoapp(iss);
                }
                if (rdFileCal.isSelected()) {
                 //   System.out.println("Came to if");
                    is.setSettings(ss);
                    is.setRight_Camera(ss.getRightCam());
                    is.setLeft_Camera(ss.getLeftCam());
                    is.setIsettings(iss);
                    gotoapp(iss);
                }
                else{
                    gotoapp(iss);
                }
            }
        }

////        {
//        FileInputStream fileIn = null;
//        try {
//            SurfaceSettings set;
//            fileIn = new FileInputStream("settings.ser");
//            ObjectInputStream in = new ObjectInputStream(fileIn);
//            set = (SurfaceSettings) in.readObject();
//            in.close();
//            fileIn.close();
//
//            if (i == 1) {
//                ProjectorGUINew fg = new ProjectorGUINew();
//                fg.setSettings(set);
//                fg.setLeft_Camera(set.getLeftCam());
//                fg.setRight_Camera(set.getRightCam());
//                fg.loadPre();
//                fg.setVisible(true);
//                this.setVisible(false);
//            }
//
//            if (i == 3) {
//                PowerPointApp ppapp = new PowerPointApp();
//                ppapp.setRcam(set.getRightCam());
//                ppapp.setLcam(set.getLeftCam());
//                ppapp.setVisible(true);
//            }
//
//        } catch (ClassNotFoundException ex) {
//            Logger.getLogger(InitialScreen.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (IOException ex) {
//            Logger.getLogger(InitialScreen.class.getName()).log(Level.SEVERE, null, ex);
//        } finally {
//            try {
//                fileIn.close();
//            } catch (IOException ex) {
//                Logger.getLogger(InitialScreen.class.getName()).log(Level.SEVERE, null, ex);
//            }
//        }

    }//GEN-LAST:event_btNextActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitButtonActionPerformed

    private void rdCalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdCalActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rdCalActionPerformed

    private void rdFileCalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdFileCalActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rdFileCalActionPerformed

    private void rdFileCamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdFileCamActionPerformed
        if (rdFileCam.isSelected() && fhwp.isSelected()) {
            rdFileCal.setEnabled(true);
            rdLastCal.setEnabled(false);
            rdInit.setEnabled(true);
            if (rdLastCal.isSelected()) {
                rdFileCal.setSelected(true);
            }
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_rdFileCamActionPerformed

    private void rdLastCamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdLastCamActionPerformed
        if (rdLastCam.isSelected() && fhwp.isSelected()) {
            rdLastCal.setEnabled(true);
            rdFileCal.setEnabled(false);
            rdInit.setEnabled(true);
            if (rdFileCal.isSelected()) {
                rdLastCal.setSelected(true);
            }
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_rdLastCamActionPerformed

    private void rdLastCalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdLastCalActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rdLastCalActionPerformed

    private void FingerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FingerButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_FingerButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing

        System.exit(0);

    }//GEN-LAST:event_formWindowClosing

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        try {

            FileInputStream fis = new FileInputStream("settings.ser");
            try {
                ObjectInputStream ois = new ObjectInputStream(fis);
                try {
                    inits = (InitialSettings) ois.readObject();
                    ois.close();
                    fis.close();
                     fs = inits.fingerSelected;
                     cs = inits.camSelectd;
                    ctd = inits.calibrateNow;
                    int app = inits.application;
                    // System.out.println("fs=" + fs + " cs=" + cs + " ctd=" + ctd + " app=" + app);
                    FingerButton.setSelected(fs);
                    redButton.setSelected(!fs);
                    rdInit.setSelected(cs);
                    rdCal.setSelected(ctd);
                    rdLastCam.setSelected(inits.cameraLastSettings);
                    rdFileCam.setSelected(inits.cameraLoadFromFile);
                    rdLastCal.setSelected(inits.calibrateLastSettings);
                    rdFileCal.setSelected(inits.calibrateLoadFromFile);
                    cameraSet();

                    switch (app) {
                        case 1:
                            fhwp.setSelected(true);
                            break;
                        case 2:
                            fh.setSelected(true);
                            buttonGroup3.clearSelection();
                            calButtonEnabling(false);
                            break;
                        case 3:
                            pp.setSelected(true);
                            buttonGroup3.clearSelection();
                            calButtonEnabling(false);
                            break;

                    }
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(InitialScreen.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (IOException ex) {
                Logger.getLogger(InitialScreen.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(InitialScreen.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_formWindowOpened

    private void ppActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ppActionPerformed
       
    }//GEN-LAST:event_ppActionPerformed

    private void fhwpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fhwpActionPerformed
        calButtonEnabling(true);
        rdCal.setSelected(ctd);
        rdLastCal.setSelected(inits.calibrateLastSettings);
        rdFileCal.setSelected(inits.calibrateLoadFromFile);

        cameraSet();

    }//GEN-LAST:event_fhwpActionPerformed

    private void fhActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fhActionPerformed
        buttonGroup3.clearSelection();
        calButtonEnabling(false);
    }//GEN-LAST:event_fhActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Toolkit toolkit = Toolkit.getDefaultToolkit();
                Dimension dim = toolkit.getScreenSize();

                InitialScreen initscreen = new InitialScreen();
                int x_location = (dim.width - initscreen.getBounds().width) / 2;
                int y_location = (dim.height - initscreen.getBounds().height) / 2;
                initscreen.setLocation(x_location, y_location);
                initscreen.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton FingerButton;
    private javax.swing.JButton btNext;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.ButtonGroup buttonGroup4;
    private javax.swing.JButton exitButton;
    private javax.swing.JRadioButton fh;
    private javax.swing.JRadioButton fhwp;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton pp;
    private javax.swing.JRadioButton rdCal;
    private javax.swing.JRadioButton rdFileCal;
    private javax.swing.JRadioButton rdFileCam;
    private javax.swing.JRadioButton rdInit;
    private javax.swing.JRadioButton rdLastCal;
    private javax.swing.JRadioButton rdLastCam;
    private javax.swing.JRadioButton redButton;
    // End of variables declaration//GEN-END:variables

    public void propertyChange(PropertyChangeEvent evt) {
        try {
            if (100 == (Integer) evt.getNewValue()) {
                this.setVisible(true);
            }
        } catch (ClassCastException e) {
        }
    }

    public void gotoapp(InitialSettings is) {
        int app = is.application;

        switch (app) {
            case 1:
                ProjectorGUINew pg = new ProjectorGUINew();
                pg.setVisible(true);
                pg.setLeft_Camera(ss.getLeftCam());
                pg.setRight_Camera(ss.getRightCam());
                System.out.println(ss.getA());
                System.out.println(ss.getC());
                pg.setIsettings(iss);
                pg.setSettings(ss);
                pg.loadPre();
                break;
            case 2:
                FreeHandGUI fhg = new FreeHandGUI();
                fhg.setVisible(true);
                fhg.setLeft_Camera(ss.getLeftCam());
                fhg.setRight_Camera(ss.getRightCam());
                fhg.setIsettings(iss);
                fhg.setSettings(ss);
                fhg.loadPre();
                break;
            case 3:
                PowerPointApp pap = new PowerPointApp();
                pap.setIsettings(iss);
                pap.setSettings(ss);
                pap.setRcam(ss.getRightCam());
                pap.setLcam(ss.getLeftCam());
                pap.setIsFingerSet(iss.fingerSelected);
                Toolkit toolkit = Toolkit.getDefaultToolkit();
                Dimension dim = toolkit.getScreenSize();

                int x_location = (dim.width - pap.getBounds().width) / 2;
                int y_location = (dim.height - pap.getBounds().height) / 2;
                pap.setLocation(x_location, y_location);
                pap.setVisible(true);
                break;

        }

        this.setVisible(false);


    }

    public void gotoSetCam() {
        SetCameras sc = new SetCameras(iss.application, 0);
        sc.setVisible(true);
        sc.setIs(iss);
    }

    public void calButtonEnabling(boolean b) {

        rdCal.setEnabled(b);
        rdFileCal.setEnabled(b);
        rdLastCal.setEnabled(b);
    }

    public void cameraSet() {

        if (rdInit.isSelected()) {
            rdLastCal.setEnabled(false);
            rdFileCal.setEnabled(false);
            rdCal.setSelected(true);
        }
        if (rdLastCam.isSelected()) {
            rdLastCal.setEnabled(true);
            rdFileCal.setEnabled(false);
            rdInit.setEnabled(true);
            if (rdFileCal.isSelected()) {
                rdLastCal.setSelected(true);
            }
        }
        if (rdFileCam.isSelected()) {
            rdFileCal.setEnabled(true);
            rdLastCal.setEnabled(false);
            rdInit.setEnabled(true);
            if (rdLastCal.isSelected()) {
                rdFileCal.setSelected(true);
            }
        }
    }
}
